package com.cndw.kungfu.model.skill;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.core.cache.CacheSkill;
import com.cndw.kungfu.domain.BuffMode;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Skill;
import com.cndw.kungfu.domain.SkillMode;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.goods.PlayerBoxs;
import com.cndw.kungfu.vo.SkillGirdVO;
import com.cndw.kungfu.vo.SkillModeVO;
import com.cndw.kungfu.vo.goodsvo.GoodsVO;

/**
 * 技能
 * 
 * @author fantadust
 * @date 2011-5-5 下午09:23:15
 */
@Component
public class SkillServer {
	@Autowired
	public SkillManger skillManger;
	@Autowired
	private CacheSkill cacheSkill;

	/**
	 * 获取技能模型
	 * 
	 * @param skillId
	 *            技能的唯一id.
	 * @return
	 */
	public SkillMode getSkillMode(int skillId) {
		return cacheSkill.getOne(skillId);
	}

	public BuffMode getBuffMode(int buffId) {
		return cacheSkill.getBuffMode(buffId);
	}

	/**
	 * 获取全部气功
	 * 
	 * @return
	 */
	public List<SkillMode> getStrengthSkills() {
		return cacheSkill.getStrengthSkills();
	}

	public Collection<SkillMode> getSkillModeMap() {
		return cacheSkill.getAll();
	}


	public BuffMode getBuffMode(int buffLibId, int level) {
		return cacheSkill.getBuffMode(buffLibId, level);
	}

	/**
	 * 获取我的技能对象
	 * 
	 * @param playerId
	 * @return
	 */
	public PlayerSkill getPlayerSkill(long playerId) {
		if (!Context.getDataServer().getPlayerSkillMap().containsKey(playerId)) {
			Map<Integer, Skill> map = skillManger.getSkillByPlayerIdFromDb(playerId);
			PlayerSkill playerSkill = new PlayerSkill(playerId);
			playerSkill.setSkillMap(map);
			playerSkill.initShortcut();
			Context.getDataServer().getPlayerSkillMap().putIfAbsent(playerId, playerSkill);
		}
		return Context.getDataServer().getPlayerSkillMap().get(playerId);
	}

	public int getQueueMax(long playerId) {
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		return player.getLevel() >= 20 ? 2 : 1;

	}

	/**
	 * 学习技能
	 * 
	 * @param playerId
	 * @param id
	 * @return
	 */
	public Skill studySkill(long playerId, int id) {

		SkillMode skillMode = getSkillMode(id);
		if (skillMode == null)
			return null;
		int skillLibId = skillMode.getSkillLibId();

		PlayerSkill playerSkill = getPlayerSkill(playerId);

		Map<Integer, Skill> skillMap = playerSkill.getSkillMap();
		Skill skill = null;
		if (skillMap.containsKey(skillLibId)) {
			skill = skillMap.get(skillLibId);
		}

		if (skill == null) {
			skill = new Skill();
			skill.setPlayerId(playerId);
			skill.setSkillLibId(skillLibId);
		}
		skill.setLevel(skillMode.getLevel());
		skill.setSkillId(skillMode.getId());
		skill.setAddTime(TimeProcesser.getUnixTime());
		skillManger.insert(skill);
		skillMap.put(skill.getSkillLibId(), skill);

		if (skillMode.getType() == SkillMode.typeVocational || skillMode.getType() == SkillMode.typeCheats) {// 自動穿戴
			int position = 0;
			boolean haveOld = false;
			for (SkillGird e : playerSkill.getShortcutGirds()) {
				if (e.getId() >= Constants.skillDifferenceGoods) {
					SkillMode eMode = getSkillMode(e.getId());
					if (eMode == null) {
						continue;
					}
					if (eMode.getSkillLibId() == skillMode.getSkillLibId()) {

						position = e.getPosition();
						haveOld = true;
						break;
					}
				}
			}
			if (position == 0) {
				for (int i = 1; i <= PlayerSkill.skillGirdMax; i++) {
					if (playerSkill.shortcutGet(i) == null) {
						position = i;
						break;
					}
				}
			}
			if (position > 0) {
				SkillGird skillGird = new SkillGird();
				skillGird.setPosition(position);
				skillGird.setId(id);
				skillGird.setAreGoods(false);
				if (haveOld || skillMode.getPassiveType() == Constants.skillAddBlood) {
					playerSkill.shortcutPut(skillGird);
				}else {
					playerSkill.shortcutOrder(skillGird);
				}
				

			}
		}
		if (skillMode.getType() == SkillMode.typeStrength || skillMode.getType() == SkillMode.typeMartial) {
			Player player = Context.getPlayerServer().getByPlayerId(playerId);
			Context.getPlayerServer().clacProperties(player);
		}

		return skill;
	}

	public List<SkillModeVO> loadSkillModeVOs(long playerId, int skillType) {
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (player == null) {
			return null;
		}
		List<SkillModeVO> skillList = new ArrayList<SkillModeVO>();

		PlayerSkill playerSkill = getPlayerSkill(playerId);
		Map<Integer, Skill> skillMap = playerSkill.getSkillMap();

		Set<Integer> skillLibIds;
		if (skillType == SkillMode.typeVocational) {
			skillLibIds = cacheSkill.getVocationalLibIds(player.getVocationId());
		} else {
			skillLibIds = cacheSkill.getLibIds(skillType);
		}
		if (skillLibIds == null || skillLibIds.size() == 0) {
			return skillList;
		}
		for (Integer libId : skillLibIds) {
			int level = 0;// 级别

			Skill skill = skillMap.get(libId);
			if (skill != null) {
				level = skill.getLevel();
			}
			if (level >= 1) {
				SkillMode currentMode = cacheSkill.getSkillMode(libId, level);
				if (currentMode != null) {
					SkillModeVO skillModeVO = currentMode.toSkillModeVO();
					skillList.add(skillModeVO);
				}
			}

			SkillMode nextMode = cacheSkill.getSkillMode(libId, level + 1);
			if (nextMode != null) {
				SkillModeVO skillModeVO = nextMode.toSkillModeVO();
				skillModeVO.initStudyBook(playerId);
				skillList.add(skillModeVO);
			}

		}

		return skillList;
	}

	/**
	 * 加载技能栏
	 * 
	 * @param playerId
	 * @return
	 */
	public List<SkillGirdVO> loadShortcut(long playerId) {
		PlayerSkill playerSkill = getPlayerSkill(playerId);
		PlayerBoxs playerBoxs = Context.getGoodsServer().getPlayerBoxs(playerId);
		Collection<SkillGird> skillGirds = playerSkill.getShortcutGirds();
		List<SkillGirdVO> skillGirdVOs = new ArrayList<SkillGirdVO>();
		for (SkillGird e : skillGirds) {
			SkillGirdVO ee = new SkillGirdVO();
			ee.setId(e.getId());
			ee.setPosition(e.getPosition());
			ee.setAreGoods(e.isAreGoods());
			if (e.isAreGoods()) {
				GoodsVO goodsVO = new GoodsVO(e.getId(), playerId, 0);
				goodsVO.setStackNum(playerBoxs.getStackNum(e.getId()));
				ee.setGoodsVO(goodsVO);

			} else {
				SkillMode skillMode = getSkillMode(e.getId());
				if (skillMode == null) {
					continue;
				}
				Skill skill = playerSkill.getSkillMap().get(skillMode.getSkillLibId());
				if (skill != null) {
					ee.setSkillVO(skill.toSkillVO());
				}

			}
			skillGirdVOs.add(ee);
		}
		return skillGirdVOs;

	}

}
